package top.frsf.plants;

import top.frsf.GamePanel;
import top.frsf.bullets.PesBullet;
import top.frsf.envi.EnvStart;
import top.frsf.envi.Grand;
import top.frsf.iface.IDelayMovie;
import top.frsf.zombies.ZomObject;

import java.awt.*;

/**
 * @BelongsProject: MiniPZ
 * @BelongsPackage: top.frsf.plants
 * @Author: FRSF
 * @CreateTime: 2022-09-07  12:31
 * @Description: 豌豆射手
 * @Version: 1.0
 */
public class Pease extends PlantsObject implements IDelayMovie {


    static {
        normal = new Image[13];
        for (int i = 0; i < 13; i++) {
            normal[i] = GamePanel.tKit.getImage("src/main/resources/plants/" +
                    "pease/Peashooter_" + i + ".png ");
        }
    }

    public Pease(String img, int x, int y, int width, int height) {
        super(img, x, y, width, height, 50);
    }

    @Override
    public void shoot() {
        if (GamePanel.fps % 70 == 1) {
            // 攻击间隙
            for (ZomObject zom : GamePanel.zomStart.zomList) {
                double abs = Math.abs(getRec().getCenterY() - zom.getRec().getCenterY());
                if (abs < 30) {
                    // 同一行有zom，开启攻击模式
                    GamePanel.bulStart.pesBulList.add(new PesBullet("src/main/resources/bullets/pease.png",
                            getX() + getWidth() - 8, getY() + 5, 30, 30));
                    break;
                }
            }
        }
    }

    @Override
    public void downHP(int hp) {
        this.hp -= hp;
    }

    @Override
    public void paintSelf(Graphics g) {
        g.setColor(new Color(232, 14, 14));
        g.drawRect(getX(), getY(), getWidth(), getHeight());
        delayMovie(count, normal, g, getX(), getY(), 5);
        shoot();

        if (hp < 0) {
            // 植物死亡，添加删除列表，重置草地
            GamePanel.plaStart.delPlaList.add(this);
            for (Grand grand : EnvStart.grandList) {
                if (grand.getRec().contains(getRec())) {
                    grand.plant = true;
                    break;
                }
            }
        }
    }

}
